package gui;

import jade.wrapper.ContainerController;
import jade.wrapper.StaleProxyException;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;

@SuppressWarnings("serial")
public class DialogDiagrammi extends JDialog {

	JPanel p = new JPanel();
	JPanel p1 = new JPanel();
	JPanel p2 = new JPanel();
	JRadioButton arrPart;
	JRadioButton coda;
	ButtonGroup group;
	ContainerController cc;
	JRadioButton utAP;
	JRadioButton uBff;
	JFormattedTextField indiceInizio;
	JFormattedTextField indiceFine;
	JLabel labelInizio;
	JLabel labelFine;

	public DialogDiagrammi(MainFrame frame, ContainerController cc) {
		super(frame);
		setTitle("Scegli diagrammi ");
		setResizable(false);
		setModal(true);
		this.setLayout(new BorderLayout());
		this.cc = cc;
		p.setLayout(new GridBagLayout());
		p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 10, 10));
		p2.setLayout(new GridBagLayout());
		group = new ButtonGroup();
		init();
		initButtons();
		add(p, BorderLayout.CENTER);
		add(p1, BorderLayout.SOUTH);
		// add(p2, BorderLayout.EAST);
		pack();
		this.setLocationRelativeTo(frame);
		setVisible(true);
	}

	private void initButtons() {
		JButton ok = new JButton("OK");
		ok.setAction(new OKAction());
		JButton annulla = new JButton("Annulla");
		annulla.setAction(new AnnullaAction());
		p1.add(ok);
		this.getRootPane().setDefaultButton(ok);
		p1.add(annulla);
	}

	private class AnnullaAction extends AbstractAction {

		public AnnullaAction() {
			super("Annulla");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			DialogDiagrammi.this.dispose();
		}

	}

	private class OKAction extends AbstractAction {

		public OKAction() {
			super("OK");
			putValue(Action.ACCELERATOR_KEY,
					KeyStroke.getKeyStroke("control D"));
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			double inizio = 0;
			double fine = -1;
			if (!indiceInizio.getText().equals(" ")) {
				inizio = Double.parseDouble(sostituisciVirgola(indiceInizio
						.getText()));
			} 
			if (!indiceFine.getText().equals(" ")) {
				fine = Double.parseDouble(sostituisciVirgola(indiceFine
						.getText()));
			}
			System.out.println("INIZIO "+inizio);
			System.out.println("FINE "+fine);
			if (arrPart.isSelected()) {
				try {
					Object[] o = { 0,inizio,fine };
					cc.createNewAgent("Requestor", "agenti.RichiediGrafico", o)
							.start();
					dispose();
				} catch (StaleProxyException e1) {
					e1.printStackTrace();
				}
			} else if (coda.isSelected()) {
				try {
					Object[] o = { 1,inizio,fine};
					cc.createNewAgent("Requestor", "agenti.RichiediGrafico", o)
							.start();
					dispose();
				} catch (StaleProxyException e1) {
					e1.printStackTrace();
				}
			} else if (utAP.isSelected()) {
				try {
					Object[] o = { 3,inizio,fine };
					cc.createNewAgent("Requestor", "agenti.RichiediGrafico", o)
							.start();
					dispose();
				} catch (StaleProxyException e1) {
					e1.printStackTrace();
				}
			} else if (uBff.isSelected()) {
				try {
					Object[] o = { 2,inizio,fine};
					cc.createNewAgent("Requestor", "agenti.RichiediGrafico", o)
							.start();
					dispose();
				} catch (StaleProxyException e1) {
					e1.printStackTrace();
				}
			}
		}
	}

	private void init() {
		arrPart = new JRadioButton("Tempi di arrivo e partenza");
		coda = new JRadioButton("Tempi di attesa in coda");
		utAP = new JRadioButton("Utenti arrivati e partiti");
		uBff = new JRadioButton("Utenti presenti nel buffer");
		group.add(arrPart);
		group.add(coda);
		group.add(utAP);
		group.add(uBff);
		arrPart.setSelected(true);
		p.add(arrPart, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));
		p.add(coda, new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));
		p.add(utAP, new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));
		p.add(uBff, new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		Box intervallo = Box.createHorizontalBox();
		intervallo.add(p2);
		intervallo.setBorder(BorderFactory
				.createTitledBorder("Scegli intervallo"));

		indiceFine = new JFormattedTextField(
				new RegexFormatter("[0123456789]*"));
		indiceFine.setColumns(4);

		indiceInizio = new JFormattedTextField(new RegexFormatter(
				"[0123456789]*"));
		indiceInizio.setColumns(4);

		labelInizio = new JLabel("Utente/Istante inizio");

		labelFine = new JLabel("Utente/Istante fine");

		p2.add(labelInizio, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		p2.add(indiceInizio, new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));

		p2.add(labelFine, new GridBagConstraints(0, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));


		p2.add(indiceFine, new GridBagConstraints(1, 1, 1, 1, 1.0, 1.0,
				GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(
						10, 0, 5, 10), 0, 0));


		DialogDiagrammi.this.add(intervallo, BorderLayout.EAST);

	}

	private String sostituisciVirgola(String s) {
		char[] x = s.toCharArray();
		for (int i = 0; i < x.length; i++) {
			if (x[i] == ',') {
				x[i] = '.';
			}
		}
		return new String(x);
	}
}